package org.scc.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.scc.model.Article;
import org.scc.service.ArticleService;
import org.scc.vo.PageVO;

public class ArticleAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private ArticleService articleService;
	private Article article;
	private int at;
	public ArticleAction(){
		articleService = new ArticleService();
	}

	public void writeArticle(){
		article.setDate(new Date() );
		article.setAuthor(getCurrentUser());
		
		articleService.add(article);
		responseSuccess();
	}
	
	public void findUserArticlePage(){
		PageVO<Article> pageVO = articleService.getPageByUser(getCurrentUser().getId(), getRowBeginIndex(), rows); 
		responeString(getMyArticleJSON(pageVO));
	}
	
	private String getMyArticleJSON(PageVO<Article> pageVO){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		JSONArray arr = new JSONArray();
		JSONObject obj;
		List<Article> articles = pageVO.getData();
		for(Article article : articles){
			obj = new JSONObject();
			obj.put("id", article.getId() );
			obj.put("title", article.getTitle() );
			obj.put("date", article.getDateText(sdf));
			obj.put("type", article.getArticleType().getName() );
			
			arr.add(obj);
		}
		obj = new JSONObject();
		obj.put("total", pageVO.getTotal());
		obj.put("rows", arr);
		return obj.toString();
	}
	
	public void findAllArticle(){
		PageVO<Article> pageVO = articleService.findPageByType(at, getRowBeginIndex(), rows); 
		responeString(getArticleJSON(pageVO));
	}
	
	private String getArticleJSON(PageVO<Article> pageVO){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		JSONArray arr = new JSONArray();
		JSONObject obj;
		List<Article> articles = pageVO.getData();
		StringBuffer sb = new StringBuffer();
		for(Article article : articles){
			obj = new JSONObject();
			obj.put("id", article.getId() );
			sb.append("<a href='../article/viewArticle.do?id=").append(article.getId()).append("' >").append(article.getTitle()).append("</a>");
			obj.put("title", sb.toString() );
			obj.put("date", article.getDateText(sdf));
			obj.put("author", article.getAuthor().getName() );
			obj.put("type", article.getArticleType().getName() );
			
			arr.add(obj);
			sb.setLength(0);
		}
		obj = new JSONObject();
		obj.put("total", pageVO.getTotal());
		obj.put("rows", arr);
		return obj.toString();
	}
	
	public String viewArticle(){
		article = articleService.load(id);
		return SUCCESS;
	}
	
	public String initEditPage(){
		article = articleService.load(id);
		return SUCCESS;
	}
	
	public void updateArticle(){
		articleService.update(article);
		responseSuccess();
	}
	
	public void deleteArticle(){
		articleService.delete(ids);
		responseSuccess();
	}
	
	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public int getAt() {
		return at;
	}

	public void setAt(int at) {
		this.at = at;
	}
	
}
